package ui;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;


/**
 * A special panel that holds and displays the stack hierarchy of available views
 * based on user navigation and input.
 * <p>
 * As a user navigates deeper into more specialized areas of the application, the
 * stack will grow and the panel will display more tabs corresponding to these
 * areas.  Sometimes, a tab can be added at the end of the stack.  However, if the
 * user opens up a new view based on a view somewhere in the middle of the stack,
 * then views will be popped off the stack until the current view is at the top of
 * the stack.  Then the new view is added onto the stack and displayed.
 * 
 * @author Aaron Chen
 * @version 3/14/2014
 */
@SuppressWarnings("serial")
public class DisplayPanel extends JTabbedPane {
  
    
  /**
   * Create the display stack panel.
   */
  public DisplayPanel() {
    super();
  }
  
//  /**
//   * Adds a component to the main application window's scrollpane display without modifying
//   * the stack.
//   * @param the_view The component to be shown next in the scrollpane.
//   */
//  public void addToStack(final MainComponent the_view) {
//    addTab(the_view.getPanelLabel(), createScrollPane(the_view));
//    setSelectedIndex(getTabCount() - 1);
//  }
  
  /**
   * Adds a component to the main application window's scrollpane display.
   * @param the_view The component to be shown next in the scrollpane.
   */
  public void addToStack(final MainComponent the_view) {
    removeFromStackUpTo();
    final JScrollPane sp = createScrollPane(the_view);
    addTab(the_view.getPanelLabel(), sp);
    setSelectedIndex(getTabCount() - 1);
    //For exceptionally large components, this sets the view to the top left. 
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          sp.getVerticalScrollBar().setValue(0);
          sp.getHorizontalScrollBar().setValue(0);
        } catch (final HeadlessException e) {
          e.printStackTrace();
        }
      }
    });
  }
  
  /**
   * Removes all components on top of the specified component from the display stack.
   */
  public void removeFromStackUpTo() {
    final int n = getSelectedIndex();
    while (getTabCount() > n + 1) {
      remove(n + 1);
    }
  }
  
  /**
   * Removes the selected component and all components on top from the display stack.
   */
  public void removeFromStack() {
    removeFromStackUpTo();
    final int n = getSelectedIndex();
    setSelectedIndex(n - 1); 
    remove(n);
  }

  /**
   * Creates a scroll pane with the component inside.
   * @param the_comp The component to be scrollable.
   * @return The scroll pane.
   */
  private JScrollPane createScrollPane(final MainComponent the_comp) {
    final JScrollPane scroll_pane = new JScrollPane((Component) the_comp);
    scroll_pane.getViewport().setBackground(Standards.COLOR_BACKGROUND);
    scroll_pane.getHorizontalScrollBar().setUnitIncrement(Standards.SCROLL_BAR_INCREMENT);
    scroll_pane.getVerticalScrollBar().setUnitIncrement(Standards.SCROLL_BAR_INCREMENT);
    return scroll_pane;
  }

}
